package iha.auh.kds.aba.painratingapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class PainRatingActivity extends FragmentActivity {
	
	private static final int NUM_PAGES = 2;
	private ScreenSlidePagerAdapter mPagerAdapter;
	private ViewPager mPager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pain_rating);
		
		int fragmentPosition = 0;
		Intent intent = getIntent();
		if (intent != null)
			fragmentPosition = intent.getExtras().getInt("FragmentPosition");
		
		mPager = (ViewPager) findViewById(R.id.pain_rating_viewpager);		
		mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
		mPager.setAdapter(mPagerAdapter);
		mPager.setCurrentItem(fragmentPosition);
	}

	public void gotoConfirm(View view) {
		int currentFragment = mPager.getCurrentItem();
		TextView scoreTextView = null;
		
		if (currentFragment == 0)
			scoreTextView = (TextView) findViewById(R.id.simple_pain_rating_score);
		else if (currentFragment == 1)
			scoreTextView = (TextView) findViewById(R.id.advanced_pain_rating_score);
		
		String score = scoreTextView.getText().toString();
		
		Intent confirmIntent = new Intent(this, ConfirmScoringActivity.class);
		confirmIntent.putExtra("score", score);
		startActivity(confirmIntent);
	}
	
	@Override
	public void onBackPressed() {
		if (mPager.getCurrentItem() == 0) {
			super.onBackPressed();
		} else {
			mPager.setCurrentItem(mPager.getCurrentItem() - 1);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.pain_rating, menu);
		return true;
	}

	private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
		public ScreenSlidePagerAdapter(FragmentManager fm) {
			super(fm);
		}
		
		@Override
		public Fragment getItem(int position) {
			switch(position) {
			case 0: return new SimplePainRatingFragment();
			case 1: return new AdvancedPainRatingFragment();
			default: return new SimplePainRatingFragment();
			}
		}

		@Override
		public int getCount() {
			return NUM_PAGES;
		}
	} 
	
}